package com.aifast.system.log.controller;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.system.oshi.CpuInfo;
import cn.hutool.system.oshi.OshiUtil;
import com.aifast.common.base.Controller;
import com.aifast.common.base.R;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import oshi.hardware.GlobalMemory;
import oshi.software.os.FileSystem;
import oshi.software.os.OSFileStore;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * @Author fujian
 * @since 2022/10/16 15:23
 */
@RestController
@RequestMapping("/log/service")
public class ServiceController extends Controller {

    @GetMapping("getCpu")
    public R getCpu(){
        CpuInfo info = OshiUtil.getCpuInfo();
        List<Map<Object,Object>> list = new ArrayList<>();
        list.add(MapUtil.builder().put("key","CPU核心数").put("value",info.getCpuNum()).build());
        list.add(MapUtil.builder().put("key","CPU总的使用率").put("value",info.getToTal()).build());
        list.add(MapUtil.builder().put("key","CPU系统使用率").put("value",info.getSys()).build());
        list.add(MapUtil.builder().put("key","CPU用户使用率").put("value",info.getUsed()).build());
        list.add(MapUtil.builder().put("key","CPU当前等待率").put("value",info.getWait()).build());
        list.add(MapUtil.builder().put("key","CPU当前空闲率").put("value",info.getFree()).build());
        list.add(MapUtil.builder().put("key","CPU利用率").put("value", NumberUtil.decimalFormat("0.00",100 - info.getFree())).build());
        list.add(MapUtil.builder().put("key","CPU型号信息").put("value",info.getCpuModel()).build());
        return OK(list);
    }

    @GetMapping("getNc")
    public R getNc(){
        GlobalMemory memory = OshiUtil.getMemory();
        List<Map<Object,Object>> list = new ArrayList<>();
        list.add(MapUtil.builder().put("key","总内存").put("value",NumberUtil.decimalFormat("0.00",(memory.getTotal() / 1024/1024/1000)) + "G").build());
        list.add(MapUtil.builder().put("key","已用内存").put("value",NumberUtil.decimalFormat("0.00",((memory.getTotal() - memory.getAvailable()) / 1024/1024/1000)) + "G").build());
        list.add(MapUtil.builder().put("key","剩余内存").put("value",NumberUtil.decimalFormat("0.00",(memory.getAvailable() / 1024/1024/1000)) + "G").build());
        return OK(list);
    }

    @GetMapping("getFwq")
    public R getFwq(){
        List<Map<Object,Object>> list = new ArrayList<>();
        Properties props = System.getProperties();

        list.add(MapUtil.builder().put("key","操作系统").put("value",props.getProperty("os.name")).build());
        list.add(MapUtil.builder().put("key","系统架构").put("value",props.getProperty("os.arch")).build());
        return OK(list);
    }

    @GetMapping("getXnj")
    public R getXnj(){
        List<Map<Object,Object>> list = new ArrayList<>();
        Properties props = System.getProperties();

        list.add(MapUtil.builder().put("key","操作系统").put("value",props.getProperty("os.name")).build());
        list.add(MapUtil.builder().put("key","系统架构").put("value",props.getProperty("os.arch")).build());
        return OK(list);
    }


    @GetMapping("getCp")
    public R getCp(){
        List<Map<Object,Object>> list = new ArrayList<>();
        FileSystem fileSystem = OshiUtil.getOs().getFileSystem();
        List<OSFileStore> fsArray = fileSystem.getFileStores();
        for (OSFileStore fs : fsArray)
        {
            long free = fs.getUsableSpace();
            long total = fs.getTotalSpace();
            long used = total - free;
            Map<Object, Object> build = MapUtil.builder().put("dirName", fs.getMount())
                    .put("fileType", fs.getType())
                    .put("typeName", fs.getName())
                    .put("total", convertFileSize(total))
                    .put("free", convertFileSize(free))
                    .put("used", convertFileSize(used))
                    .put("usage", NumberUtil.decimalFormat("#.##%",used * 1.00/ total* 1.00)).build();
            list.add(build);
        }

//        list.add(MapUtil.builder().put("key","操作系统").put("value",OshiUtil.getOs()).build());
//        list.add(MapUtil.builder().put("key","系统架构").put("value",SystemUtil.getOsInfo().getArch()).build());
//        list.add(MapUtil.builder().put("key","IP").put("value","").build());
        return OK(list);
    }
    /**
     * 字节转换
     *
     * @param size 字节大小
     * @return 转换后值
     */
    public String convertFileSize(long size)
    {
        long kb = 1024;
        long mb = kb * 1024;
        long gb = mb * 1024;
        if (size >= gb)
        {
            return String.format("%.1f GB", (float) size / gb);
        }
        else if (size >= mb)
        {
            float f = (float) size / mb;
            return String.format(f > 100 ? "%.0f MB" : "%.1f MB", f);
        }
        else if (size >= kb)
        {
            float f = (float) size / kb;
            return String.format(f > 100 ? "%.0f KB" : "%.1f KB", f);
        }
        else
        {
            return String.format("%d B", size);
        }
    }


}
